package yamen.bdwm.data;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.analytics.tracking.android.EasyTracker;

import yamen.bdwm.MainConstant;
import yamen.bdwm.datastruct.WmBoardItem;
import yamen.bdwm.engine.HtmlParser;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;


public class WmMyData {
	public WmMyToptenList toptenlist=new WmMyToptenList();
	public MailList maillist=new MailList();
	public FavouriteList favlist=new FavouriteList();
	//public CatalogList catlist = new CatalogList();
	public WmBoardItem MainBoard=new WmBoardItem();
	public HtmlParser parser=new HtmlParser();
	public static String lz;
	public static ArrayList<String> boards = null;
	public static HashMap<String, WmBoardItem> boardmapHashMap = new HashMap<String, WmBoardItem>();
	public static HashMap<String, WmBoardItem> groupmap = new HashMap<String, WmBoardItem>();
	public static boolean is_iTopTen = false;
	public static int boardMode = 0;
	
	public static String apn = "";
	public static String proxyHost = "";
	public static int proxyPort = 80;
	public static String proxyUsername = "";
	public static String proxyPassword = "";
	
	static WmMyData instance;
	public static ArrayList<String> notices;
	public static WmMyData getinstance()
	{
		if(instance==null)instance=new WmMyData();
		return instance;
	}
	public void clearProxyData() {
		WmMyData.getinstance().proxyHost = "";
		WmMyData.getinstance().proxyPort = -1;
		WmMyData.getinstance().proxyUsername = "";
		WmMyData.getinstance().proxyPassword = "";
	}
	public static void clear()
	{
		instance = null;
	}
	public void loadData(final Context c,final onPublicDataLoaded onp){
		/*
		 try {
				MainConstant.versionCode = c.getPackageManager().getPackageInfo(c.getPackageName(), 0).versionCode;
			} catch (NameNotFoundException e) {
				MainConstant.versionCode = 0;
				e.printStackTrace();
			}
			*/
		MainConstant.versionCode = 11;
	    new AsyncTask<Void, Integer, Void>(){

			@Override
			protected Void doInBackground(Void... params) {
				notices = WmMyData.getinstance().parser.getNotices();
				WmMyData.getinstance().parser.getRemoteCatalog(false);
				SharedPreferences sp = c.getSharedPreferences("FIRSTRUN", c.MODE_PRIVATE);
				if(!sp.getBoolean("activated", false))
				{
					EasyTracker.getTracker().sendEvent("UserAction", "activate", "null", (long) 0);
					sp.edit().putBoolean("activated", true).commit();
				}
				return null;
				
			}
			protected void onPostExecute(Void p)
			{
				 if(notices != null && !notices.isEmpty())
					 onp.showNotices(true);
				 else
					 onp.showNotices(false);
				 onp.onLoaded();
			}
			}.execute();
	}
	public static interface onPublicDataLoaded
	{
		void showNotices(boolean show);
		void onLoaded();
	}
}
